using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AspNetForums;
using AspNetForums.Components;
using AspNetForums.Controls;

namespace AspNetForums.Admin
{
	/// <summary>
	/// Summary description for ForumAdmin.
	/// </summary>
	public class ForumAdmin : System.Web.UI.Page
	{
		protected Forum	_forumToEdit;

		protected System.Web.UI.WebControls.TextBox txtName;
		protected System.Web.UI.WebControls.TextBox txtDescription;
		protected System.Web.UI.WebControls.RadioButtonList optIsActive;
		protected System.Web.UI.WebControls.RadioButtonList optIsSearchable;
		protected System.Web.UI.WebControls.RadioButtonList optIsModerated;
		protected System.Web.UI.WebControls.RadioButtonList optEnablePostStatistics;
		protected System.Web.UI.WebControls.RadioButtonList optEnableAutoDelete;
		protected System.Web.UI.WebControls.TextBox txtAutoDeleteThreshold;
		protected System.Web.UI.WebControls.RadioButtonList optEnableAnonymousPosting;
		protected System.Web.UI.WebControls.Button btnSave;
		protected System.Web.UI.WebControls.Literal lblForumID;
		protected System.Web.UI.WebControls.Literal lblDateCreated;
		protected System.Web.UI.WebControls.Literal lblTotalPosts;
		protected System.Web.UI.WebControls.Literal lblTotalThreads;
		protected System.Web.UI.WebControls.Literal lblMostRecentPostID;
		protected System.Web.UI.WebControls.Literal lblMostRecentThreadID;
		protected System.Web.UI.WebControls.Literal lblMostRecentPostAuthor;
		protected System.Web.UI.WebControls.Literal lblMostRecentPostAuthorID;
		protected System.Web.UI.WebControls.Literal lblMostRecentPostDate;
		protected System.Web.UI.WebControls.Literal lblPostsToModerate;
	
		protected AspNetForums.Controls.ForumListBox listParentID;

		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
			_forumToEdit = Forums.GetForum( ForumContext.Current.ForumID, false, false, ForumContext.Current.UserID );

			lblForumID.Text					= _forumToEdit.ForumID.ToString();
			optIsActive.Items.FindByValue( _forumToEdit.IsActive.ToString() ).Selected = true;
			txtName.Text					= _forumToEdit.Name.ToString();
			txtDescription.Text				= _forumToEdit.Description.ToString();
			lblDateCreated.Text				= _forumToEdit.DateCreated.ToString();
			optIsModerated.Items.FindByValue( _forumToEdit.IsModerated.ToString()	).Selected = true;
			lblTotalPosts.Text				= _forumToEdit.TotalPosts.ToString();
			lblTotalThreads.Text			= _forumToEdit.TotalThreads.ToString();
			optEnablePostStatistics.Items.FindByValue( _forumToEdit.EnablePostStatistics.ToString() ).Selected = true;
			optEnableAutoDelete.Items.FindByValue( _forumToEdit.EnableAutoDelete.ToString() ).Selected = true;
			optEnableAnonymousPosting.Items.FindByValue( _forumToEdit.EnableAnonymousPosting.ToString() ).Selected = true;
			txtAutoDeleteThreshold.Text		= _forumToEdit.AutoDeleteThreshold.ToString();
			lblMostRecentPostID.Text		= _forumToEdit.MostRecentPostID.ToString();
			lblMostRecentThreadID.Text		= _forumToEdit.MostRecentThreadID.ToString();
			lblMostRecentPostAuthor.Text	= _forumToEdit.MostRecentPostAuthor.ToString();
			lblMostRecentPostAuthorID.Text	= _forumToEdit.MostRecentPostAuthorID.ToString();
			lblMostRecentPostDate.Text		= _forumToEdit.MostRecentPostDate.ToString();
			lblPostsToModerate.Text			= _forumToEdit.PostsToModerate.ToString();
			//			_forumType.Text	= _forumToEdit.ForumType.ToString();
			optIsSearchable.Items.FindByValue( _forumToEdit.IsSearchable.ToString() ).Selected = true;

			btnSave.Click +=new EventHandler(btnSave_Click);

			if( !Page.IsPostBack) {
				listParentID.SelectedForum		= _forumToEdit.ParentID;
				listParentID.SelectedForumGroup = _forumToEdit.ForumGroupID;
				DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void btnSave_Click(object sender, EventArgs e) {
			_forumToEdit.IsActive					= Boolean.Parse( optIsActive.SelectedItem.Value );
			_forumToEdit.ParentID					= listParentID.SelectedForum;
			_forumToEdit.ForumGroupID				= listParentID.SelectedForumGroup;
			_forumToEdit.Name						= txtName.Text;
			_forumToEdit.Description				= txtDescription.Text;
			//			_forumToEdit.DateCreated				= DateTime.Parse(_dateCreated.Text);
			_forumToEdit.IsModerated				= Boolean.Parse( optIsModerated.SelectedItem.Value );
			//			_forumToEdit.TotalPosts					= Int32.Parse(_totalPosts.Text);
			//			_forumToEdit.TotalThreads				= Int32.Parse(_totalThreads.Text);
			_forumToEdit.EnablePostStatistics		= Boolean.Parse( optEnablePostStatistics.SelectedItem.Value );
			_forumToEdit.EnableAutoDelete			= Boolean.Parse( optEnableAutoDelete.SelectedItem.Value );
			_forumToEdit.EnableAnonymousPosting		= Boolean.Parse( optEnableAnonymousPosting.SelectedItem.Value	);
			_forumToEdit.AutoDeleteThreshold		= Int32.Parse(txtAutoDeleteThreshold.Text);
			//			_forumToEdit.MostRecentPostID			= Int32.Parse(_mostRecentPostID.Text);
			//			_forumToEdit.MostRecentThreadID			= Int32.Parse(_mostRecentThreadID.Text);
			//			_forumToEdit.MostRecentPostAuthor		= _mostRecentPostAuthor.Text;
			//			_forumToEdit.MostRecentPostAuthorID		= Int32.Parse(_mostRecentPostAuthorID.Text);
			//			_forumToEdit.MostRecentPostDate			= DateTime.Parse(_mostRecentPostDate.Text);
			//			_forumToEdit.PostsToModerate			= Int32.Parse(_postsToModerate.Text);
			//			_forumToEdit.ForumType					= _forumType.Text;
			_forumToEdit.IsSearchable				= Boolean.Parse( optIsSearchable.SelectedItem.Value );

			Forums.UpdateForum(	_forumToEdit );

			listParentID.DataBind();

			// save	is complete, update	the	page data
			DataBind();

		}
	}
}
